import get from '@dfeidao/atom-web/local/get';
import fire from '@dfeidao/atom-web/msg/fire';
import nodejs from '@dfeidao/atom-web/msg/nodejs';
import error from '@dfeidao/atom-web/msgbox/error';
import success from '@dfeidao/atom-web/msgbox/success';
import { IFeidaoAiBrowserComponent } from '@dfeidao/web/interfaces';
import hide_loading from '../../atom/hide-loading';
import show_loading from '../../atom/show-loading';
interface Aa {
	id: string;
	role_no: string;
	role_name: string;
	icon: string;
	role_type_id: string;
}
export default async function a002(fd: IFeidaoAiBrowserComponent) {
	const productid = get(fd, 'productid') as string;
	const role_name = fd.data.node.querySelector<HTMLInputElement>('[name="role_name"]').value;
	const role_type_id = fd.data.node.querySelector<HTMLSelectElement>('[name="role_type_id"]').value;
	const icon = fd.data.node.querySelector<HTMLInputElement>('[name="icon"]').value;
	const obj = get(fd, 'old') as Aa;
	const _id = obj;
	if (!role_name) {
		error('角色名称不能为空');
		return;
	}
	if (!role_type_id) {
		error('请选择角色类型');
		return;
	}
	const old_name = get(fd, 'role_name') as string;
	// debugger;
	show_loading();
	const res1 = await nodejs<{ count: number }>('roler/zj-000003/s001', { role_no: obj, productid });
	hide_loading();
	if (res1.count > 0 && obj.role_type_id !== role_type_id) {
		fire('zj-000010', 'ec-001', {
			_id,
			role_name,
			role_type_id,
			icon,
			role_no: obj
		});
		return;
	}
	let query = {};
	// console.log(query);
	if (role_name === old_name) {
		query = {
			_id,
			role_type_id,
			role_name: 'aaa',
			icon,
			productid
		};
	} else {
		query = {
			_id,
			role_name,
			role_type_id,
			icon,
			productid
		};
	}
	const res = await nodejs('roler/zj-000003/s002', query);

	if (res === 'success') {
		fire('zj-000001', 'ec-002');
		fire('zj-000003', 'eu-001');
		success('保存成功');
		return;
	} else if (res === 'fail') {
		error('保存失败');
		return;
	} else {
		error('角色名称重复，无法保存');
		return;
	}
}
